/// Author:					    Joe Audette
/// Created:				    2004-08-29
/// Last Modified:			    2011-03-02
/// 
/// The use and distribution terms for this software are covered by the 
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
/// which can be found in the file CPL.TXT at the root of this distribution.
/// By using this software in any fashion, you are agreeing to be bound by 
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software. 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Web.Security;
using System.Web.UI.WebControls;
using log4net;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Business.WebHelpers.UserRegisteredHandlers;
using mojoPortal.Net;
using mojoPortal.Web.Configuration;
using mojoPortal.Web.Controls;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using Resources;

namespace mojoPortal.Web.AdminUI 
{
    public partial class ManageUsers : NonCmsBasePage
	{
        private static readonly ILog log = LogManager.GetLogger(typeof(ManageUsers));
        private Guid userGuid = Guid.Empty;
        private int pageID = -1;
        private int userID = -1;
        private string AvatarPath = string.Empty;
        private SiteUser siteUser = null;
        private SiteUser currentUser = null;
        protected Double TimeOffset = 0;
        protected TimeZoneInfo timeZone = null;
        private bool isAdmin = false;
       
        //Gravatar public enum RatingType { G, PG, R, X }
        private mojoPortal.Web.UI.Gravatar.RatingType MaxAllowedGravatarRating = SiteUtils.GetMaxAllowedGravatarRating();
        private bool allowGravatars = false;
        private bool disableAvatars = true;
        private CommerceConfiguration commerceConfig = null;
        protected string DeleteLinkImage = "~/Data/SiteImages/" + WebConfigSettings.DeleteLinkImage;

		
        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.btnUpdate.Click += new EventHandler(this.btnUpdate_Click);
            this.btnDelete.Click += new EventHandler(btnDelete_Click);
            this.addExisting.Click += new EventHandler(this.AddRole_Click);
            this.userRoles.ItemDataBound += new DataListItemEventHandler(userRoles_ItemDataBound);
            this.userRoles.ItemCommand += new DataListCommandEventHandler(this.UserRoles_ItemCommand);
            this.btnUnlockUser.Click += new EventHandler(btnUnlockUser_Click);
            this.btnLockUser.Click += new EventHandler(btnLockUser_Click);
            this.btnConfirmEmail.Click += new EventHandler(btnConfirmEmail_Click);
            btnApprove.Click += new EventHandler(btnApprove_Click);
            btnPurgeUserLocations.Click += new EventHandler(btnPurgeUserLocations_Click);
            btnResendConfirmationEmail.Click += new EventHandler(btnResendConfirmationEmail_Click);
            //btnUploadAvatar.Click += new EventHandler(btnUploadAvatar_Click);

            SuppressMenuSelection();
            SuppressPageMenu();
            EnableViewState = true;

            
        }

        

        #endregion

        private void Page_Load(object sender, EventArgs e)
		{
            
            if (SiteUtils.SslIsAvailable()) SiteUtils.ForceSsl();
            SecurityHelper.DisableBrowserCache();

            LoadSettings();


            if (this.userID > -1)
            {
                siteUser = new SiteUser(siteSettings, this.userID);
                newsLetterPrefs.UserGuid = siteUser.UserGuid;
                purchaseHx.UserGuid = siteUser.UserGuid;
                purchaseHx.ShowAdminOrderLink = true;
            }
            else
            {
                if (userGuid != Guid.Empty)
                {
                    siteUser = new SiteUser(siteSettings, userGuid);
                    newsLetterPrefs.UserGuid = siteUser.UserGuid;
                    purchaseHx.UserGuid = siteUser.UserGuid;
                    purchaseHx.ShowAdminOrderLink = true;

                }
            }

            if (userID == -1)
            {
                if ((!WebUser.IsInRoles(siteSettings.RolesThatCanCreateUsers))&&(!WebUser.IsInRoles(siteSettings.RolesThatCanManageUsers)))
                {
                    SiteUtils.RedirectToEditAccessDeniedPage();
                    return;
                }

            }
            else
            {
                if (
                    (
                   ((WebUser.IsInRoles(siteSettings.RolesThatCanManageUsers))||(WebUser.IsInRoles(siteSettings.RolesThatCanCreateUsers)))
                    && !isAdmin)
                    )
                {
                    // only admins can edit admins
                    if (siteUser.IsInRoles("Admins"))
                    {
                        SiteUtils.RedirectToEditAccessDeniedPage();
                        return;
                    }
                    
                    HideAdminControls();
                   
                }
                else
                {
                    if (!isAdmin)
                    {
                        SiteUtils.RedirectToEditAccessDeniedPage();
                        return;
                    }
                }
            }

            SetupAvatarScript();

			this.divUserGuid.Visible = false;
            divProfileApproved.Visible = false;
            divApprovedForLogin.Visible = false;

            if (
                (siteUser != null) 
                && (siteSettings.RequireApprovalBeforeLogin) 
                && (!siteUser.ApprovedForLogin) 
                && ((WebUser.IsInRoles(siteSettings.RolesThatCanCreateUsers)) || (WebUser.IsInRoles(siteSettings.RolesThatCanManageUsers)))
                )
            {
                divApprovedForLogin.Visible = true;
            }
            
            divOpenID.Visible = ((WebConfigSettings.EnableOpenIdAuthentication && siteSettings.AllowOpenIdAuth) || siteSettings.RpxNowApiKey.Length > 0);

            divWindowsLiveID.Visible = WebConfigSettings.EnableWindowsLiveAuthentication && siteSettings.AllowWindowsLiveAuth;
           
            PopulateProfileControls();
			PopulateLabels();
           
            if (!IsPostBack)
			{
                PopulateControls();
            }

            
        }

	
		private void PopulateControls()
		{
            if (!siteSettings.RequiresQuestionAndAnswer)
            {
                divSecurityQuestion.Visible = false;
                divSecurityAnswer.Visible = false;
            }

            if ((siteUser != null)&&(siteUser.UserId > -1))
			{
                spnTitle.InnerText = Resource.ManageUsersTitleLabel + " " + siteUser.Name;
                
                txtName.Text = SecurityHelper.RemoveMarkup(siteUser.Name);
                this.txtLoginName.Text = SecurityHelper.RemoveMarkup(siteUser.LoginName);

                lnkAvatarUpload.ClientClick = "return GB_showPage('" + Page.Server.HtmlEncode(string.Format(CultureInfo.InvariantCulture, Resource.UploadAvatarForUserFormat, siteUser.Name)) + "', this.href, GBCallback)";
                txtEmail.Text = siteUser.Email;
                txtOpenIDURI.Text = siteUser.OpenIdUri;
                txtWindowsLiveID.Text = siteUser.WindowsLiveId;
                txtLiveMessengerCID.Text = siteUser.LiveMessengerId;
                chkEnableLiveMessengerOnProfile.Checked = siteUser.EnableLiveMessengerOnProfile;
                gravatar1.Email = siteUser.Email;
                gravatar1.MaxAllowedRating = MaxAllowedGravatarRating;

                if (siteUser.LastActivityDate > DateTime.MinValue)
                {
                    if (timeZone != null)
                    {
                        this.lblLastActivityDate.Text = siteUser.LastActivityDate.ToLocalTime(timeZone).ToString();
                    }
                    else
                    {
                        this.lblLastActivityDate.Text = siteUser.LastActivityDate.AddHours(TimeOffset).ToString();
                    }
                }

                if (siteUser.LastLoginDate > DateTime.MinValue)
                {
                    if (timeZone != null)
                    {
                        this.lblLastLoginDate.Text = siteUser.LastLoginDate.ToLocalTime(timeZone).ToString();
                    }
                    else
                    {
                        this.lblLastLoginDate.Text = siteUser.LastLoginDate.AddHours(TimeOffset).ToString();
                    }
                }

                if (siteUser.LastPasswordChangedDate > DateTime.MinValue)
                {
                    if (timeZone != null)
                    {
                        this.lblLastPasswordChangeDate.Text = siteUser.LastPasswordChangedDate.ToLocalTime(timeZone).ToString();
                    }
                    else
                    {
                        this.lblLastPasswordChangeDate.Text = siteUser.LastPasswordChangedDate.AddHours(TimeOffset).ToString();
                    }
                }

                if (siteUser.LastLockoutDate > DateTime.MinValue)
                {
                    if (timeZone != null)
                    {
                        this.lblLastLockoutDate.Text = siteUser.LastLockoutDate.ToLocalTime(timeZone).ToString();
                    }
                    else
                    {
                        this.lblLastLockoutDate.Text = siteUser.LastLockoutDate.AddHours(TimeOffset).ToString();
                    }
                }
                this.lblFailedPasswordAttemptCount.Text = siteUser.FailedPasswordAttemptCount.ToString();
                this.lblFailedPasswordAnswerAttemptCount.Text = siteUser.FailedPasswordAnswerAttemptCount.ToString();
                this.chkIsLockedOut.Checked = siteUser.IsLockedOut;
                btnLockUser.Visible = !siteUser.IsLockedOut;
                btnUnlockUser.Visible = siteUser.IsLockedOut;

                if (siteSettings.UseSecureRegistration)
                {
                    if (siteUser.RegisterConfirmGuid == Guid.Empty)
                    {
                        chkEmailIsConfirmed.Checked = true;
                        btnConfirmEmail.Enabled = false;
                    }
                }
                else
                {
                    divEmailConfirm.Visible = false;
                }
                
                this.txtComment.Text = siteUser.Comment;
                this.txtPasswordQuestion.Text = siteUser.PasswordQuestion;
                this.txtPasswordAnswer.Text = siteUser.PasswordAnswer;
                chkRequirePasswordChange.Checked = siteUser.MustChangePwd;


                if (!siteSettings.UseLdapAuth || (siteSettings.UseLdapAuth && WebConfigSettings.UseLDAPFallbackAuthentication))
                {
                    if (siteSettings.PasswordFormat == 0)
                    { //Clear
                        this.txtPassword.Text = siteUser.Password;

                    }
                    else if (siteSettings.PasswordFormat == 2)
                    {
                        try
                        {
                            mojoMembershipProvider mojoMembership = (mojoMembershipProvider)Membership.Provider;
                            this.txtPassword.Text = mojoMembership.UnencodePassword(siteUser.Password, MembershipPasswordFormat.Encrypted);
                        }
                        catch (FormatException ex)
                        {
                            log.Error("Error decoding password for user " + siteUser.Email + " on manage users page.", ex);
                            // TODO: should we generate a random password and fix it here?
                        }
                    }

                }

                if (timeZone != null)
                {
                    lblCreatedDate.Text = siteUser.DateCreated.ToLocalTime(timeZone).ToString();
                }
                else
                {
                    lblCreatedDate.Text = siteUser.DateCreated.AddHours(TimeOffset).ToString();
                }
                lblUserGuid.Text = siteUser.UserGuid.ToString();
                lblTotalPosts.Text = siteUser.TotalPosts.ToInvariantString();
                lnkUserPosts.UserId = siteUser.UserId;
                lnkUserPosts.TotalPosts = siteUser.TotalPosts;
                lnkUnsubscribeFromForums.NavigateUrl = SiteRoot + "/Forums/UnsubscribeForum.aspx?ue=" + Page.Server.UrlEncode(siteUser.Email);

                chkProfileApproved.Checked = siteUser.ProfileApproved;
                
                chkTrusted.Checked = siteUser.Trusted;
                chkDisplayInMemberList.Checked = siteUser.DisplayInMemberList;

                if ((!allowGravatars)&&(!disableAvatars))
                {
                    if (siteUser.AvatarUrl.Length > 0)
                    {
                        imgAvatar.Src = ImageSiteRoot + "/Data/Sites/"
                            + siteSettings.SiteId.ToInvariantString() + "/useravatars/" + siteUser.AvatarUrl;
                    }
                    else
                    {
                        imgAvatar.Src = ImageSiteRoot + "/Data/SiteImages/1x1.gif";
                    }
                }

#if!MONO
                ISettingControl setting = timeZoneSetting as ISettingControl;
                if (setting != null)
                {
                    setting.SetValue(siteUser.TimeZoneId);
                }

#endif

                using (IDataReader reader = SiteUser.GetRolesByUser(siteSettings.SiteId, siteUser.UserId))
                {
                    userRoles.DataSource = reader;
                    userRoles.DataBind();
                }

                using (IDataReader reader = Role.GetRolesUserIsNotIn(siteSettings.SiteId, siteUser.UserId))
                {
                    allRoles.DataSource = reader;
                    allRoles.DataBind();
                }

                if (allRoles.Items.Count == 0)
                {
                    allRoles.Enabled = false;
                    addExisting.Enabled = false;
                    addExisting.Text = Resource.ManageUsersUserIsInAllRolesMessage;
                   
                }

                List<UserLocation> userLocations = UserLocation.GetByUser(siteUser.UserGuid);
                grdUserLocation.DataSource = userLocations;
                grdUserLocation.DataBind();

			}
			else
			{
                spnTitle.InnerText = Resource.ManageUsersAddUserLabel;
				HideExtendedProfileControls();
			}

		}

        private void HideAdminControls()
        {
            if (!WebUser.IsInRoles(siteSettings.RolesThatCanManageUsers))
            {
                divDisplayInMemberList.Visible = false;
                liProfile.Visible = false;
                tabProfile.Visible = false;
                liLocation.Visible = false;
                tabLocation.Visible = false;

                if (userID != -1)
                {
                    btnUpdate.Enabled = false;
                    btnDelete.Enabled = false;
                    txtPassword.TextMode = TextBoxMode.Password;
                    liActivity.Visible = false;
                    tabActivity.Visible = false;
                    liNewsletters.Visible = false;
                    tabNewsletters.Visible = false;
                }
            }

            liRoles.Visible = false;
            tabRoles.Visible = false;

            if (!WebUser.IsInRoles(siteSettings.CommerceReportViewRoles))
            {
                liOrderHistory.Visible = false;
                tabOrderHistory.Visible = false;
            }
           
           
            
        }

        private void PopulateProfileControls()
        {
            if (siteUser == null) { return; }
            if ((!isAdmin)&&!WebUser.IsInRoles(siteSettings.RolesThatCanManageUsers)) { return; }
            
            mojoProfileConfiguration profileConfig = mojoProfileConfiguration.GetConfig();
            if (profileConfig != null)
            {
                foreach (mojoProfilePropertyDefinition propertyDefinition in profileConfig.PropertyDefinitions)
                {

#if!MONO
                    // we are using the new TimeZoneInfo list but it doesn't work under Mono
                    // this makes us skip the TimeOffsetHours setting from mojoProfile.config which is not used under windows
                    if (propertyDefinition.Name == mojoProfilePropertyDefinition.TimeOffsetHoursKey) { continue; }
#endif
                    // we allow this to be configured as a profile property so it can be required for registration
                    // but we don't need to load it here because we have a dedicated control for the property already
                    if (propertyDefinition.Name == mojoProfilePropertyDefinition.TimeZoneIdKey) { continue; }

                    if (
                        (propertyDefinition.OnlyAvailableForRoles.Length == 0)
                        ||(siteUser.IsInRoles(propertyDefinition.OnlyAvailableForRoles))
                        )
                    {
                        object propValue = siteUser.GetProperty(propertyDefinition.Name, propertyDefinition.SerializeAs, propertyDefinition.LazyLoad);
                        if (propValue != null)
                        {
                            mojoProfilePropertyDefinition.SetupPropertyControl(
                                this,
                                pnlProfileProperties,
                                propertyDefinition,
                                propValue.ToString(),
                                TimeOffset,
                                timeZone,
                                SiteRoot);
                        }
                        else
                        {
                            mojoProfilePropertyDefinition.SetupPropertyControl(
                                this,
                                pnlProfileProperties,
                                propertyDefinition,
                                propertyDefinition.DefaultValue,
                                TimeOffset,
                                timeZone,
                                SiteRoot);
                        }
                    }
                }
            }

           
        }

       
     
        private void btnUpdate_Click(Object sender, EventArgs e)
		{
            Page.Validate("profile");
            if (Page.IsValid)
            {
                if (this.userID > -1)
                {
                    UpdateUser();
                }
                else
                {
                    CreateUser();
                }
            }
			
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if((siteUser != null)&&(this.userID > -1))
			{
                siteUser.DeleteUser();
				WebUtils.SetupRedirect(this, SiteRoot + "/MemberList.aspx");
                return;

			}

		}

		private void UpdateUser()
		{
            if (siteUser == null) { return; }

            
            if (
                (siteUser.Email != txtEmail.Text)
                && (SiteUser.EmailExistsInDB(siteSettings.SiteId, txtEmail.Text))
                )
            {
                lblErrorMessage.Text = Resource.DuplicateEmailMessage;
                return;
            }

            if (
                (siteUser.LoginName != txtLoginName.Text)
                && (SiteUser.LoginExistsInDB(siteSettings.SiteId, txtLoginName.Text))
                )
            {
                lblErrorMessage.Text = Resource.DuplicateUserNameMessage;
                return;
            }

            siteUser.Name = txtName.Text;
            siteUser.LoginName = txtLoginName.Text;
            siteUser.Email = txtEmail.Text;

            if (divOpenID.Visible)
            {
                siteUser.OpenIdUri = txtOpenIDURI.Text;
            }

            string oldPassword = siteUser.Password;

            if (!siteSettings.UseLdapAuth || (siteSettings.UseLdapAuth && WebConfigSettings.UseLDAPFallbackAuthentication))
            {
                if (txtPassword.Text.Length > 0)
                {
                    mojoMembershipProvider mojoMembership = (mojoMembershipProvider) Membership.Provider;
                    siteUser.Password = mojoMembership.EncodePassword(txtPassword.Text, siteSettings);
                }
            }

            if (WebConfigSettings.LogIpAddressForPasswordChanges)
            {
                if((siteUser.UserId != -1)&&(oldPassword != siteUser.Password))
                {
                    log.Info("password for user " + siteUser.Name + " was changed by an admin user from ip address " + SiteUtils.GetIP4Address());
                }
            }

            siteUser.ProfileApproved = chkProfileApproved.Checked;
            
            siteUser.Trusted = chkTrusted.Checked;
            siteUser.DisplayInMemberList = chkDisplayInMemberList.Checked;
            //siteUser.AvatarUrl = ddAvatars.SelectedValue;

            siteUser.MustChangePwd = chkRequirePasswordChange.Checked;
            siteUser.Comment = this.txtComment.Text;
            siteUser.PasswordQuestion = this.txtPasswordQuestion.Text;
            siteUser.PasswordAnswer = this.txtPasswordAnswer.Text;
            siteUser.WindowsLiveId = txtWindowsLiveID.Text;
            siteUser.LiveMessengerId = txtLiveMessengerCID.Text;
            siteUser.EnableLiveMessengerOnProfile = chkEnableLiveMessengerOnProfile.Checked;
#if!MONO
            ISettingControl setting = timeZoneSetting as ISettingControl;
            if (setting != null)
            {
                siteUser.TimeZoneId = setting.GetValue();
            }

#endif

            if (siteUser.Save())
            {
                mojoProfileConfiguration profileConfig = mojoProfileConfiguration.GetConfig();

                foreach (mojoProfilePropertyDefinition propertyDefinition in profileConfig.PropertyDefinitions)
                {

                    mojoProfilePropertyDefinition.SaveProperty(
                        siteUser,
                        pnlProfileProperties,
                        propertyDefinition,
                        TimeOffset,
                        timeZone);
                }

                
                if ((currentUser != null) && (currentUser.UserId == siteUser.UserId))
                {
                    if ((siteSettings.UseEmailForLogin) && (siteUser.Email != currentUser.Email))
                    {
                        FormsAuthentication.SetAuthCookie(siteUser.Email, false);
                    }

                    if ((!siteSettings.UseEmailForLogin) && (siteUser.LoginName != currentUser.LoginName))
                    {
                        FormsAuthentication.SetAuthCookie(siteUser.LoginName, false);
                    }

                }

                WebUtils.SetupRedirect(this, Request.RawUrl);
            }
            
		}

		private void CreateUser()
		{
			
            if (SiteUser.EmailExistsInDB(siteSettings.SiteId, txtEmail.Text))
            {
                lblErrorMessage.Text = Resource.DuplicateEmailMessage;
                return ;
            }

            if (SiteUser.LoginExistsInDB(siteSettings.SiteId, txtLoginName.Text))
            {
                lblErrorMessage.Text = Resource.DuplicateUserNameMessage;
                return;
            }

            SiteUser user = new SiteUser(siteSettings);
			user.Name = txtName.Text;
			user.LoginName = txtLoginName.Text;
			user.Email = txtEmail.Text;

            mojoMembershipProvider mojoMembership = (mojoMembershipProvider)Membership.Provider;
		    user.Password = mojoMembership.EncodePassword(txtPassword.Text, siteSettings);

			if(user.Save())
			{
                user.PasswordQuestion = this.txtPasswordQuestion.Text;
                user.PasswordAnswer = this.txtPasswordAnswer.Text;
                user.Save();

                mojoProfileConfiguration profileConfig = mojoProfileConfiguration.GetConfig();
                // set default values
                foreach (mojoProfilePropertyDefinition propertyDefinition in profileConfig.PropertyDefinitions)
                {
                    mojoProfilePropertyDefinition.SavePropertyDefault(user, propertyDefinition);
                }

                CacheHelper.TouchMembershipStatisticsCacheDependencyFile();

                UserRegisteredEventArgs u = new UserRegisteredEventArgs(user);
                OnUserRegistered(u);

				WebUtils.SetupRedirect(this, SiteRoot 
                    + "/Admin/ManageUsers.aspx?userId=" + user.UserId.ToInvariantString() 
					+ "&username=" + user.Email + "&pageid=" + pageID.ToInvariantString());
				return;
 
			}

		}

        protected void OnUserRegistered(UserRegisteredEventArgs e)
        {
            foreach (UserRegisteredHandlerProvider handler in UserRegisteredHandlerProviderManager.Providers)
            {
                handler.UserRegisteredHandler(null, e);
            }

           
        }

        void btnResendConfirmationEmail_Click(object sender, EventArgs e)
        {
            if (this.userID > -1)
            {
                SiteUser user = new SiteUser(siteSettings, this.userID);
                if (user.RegisterConfirmGuid != Guid.Empty)
                {
                    Notification.SendRegistrationConfirmationLink(
                    SiteUtils.GetSmtpSettings(),
                    ResourceHelper.GetMessageTemplate("RegisterConfirmEmailMessage.config"),
                    siteSettings.DefaultEmailFromAddress,
                    siteSettings.DefaultFromEmailAlias,
                    user.Email,
                    siteSettings.SiteName,
                    SiteRoot + "/ConfirmRegistration.aspx?ticket=" +
                    user.RegisterConfirmGuid.ToString());

                }
            }

            WebUtils.SetupRedirect(this, Request.RawUrl);
            return;

        }

        void btnApprove_Click(object sender, EventArgs e)
        {
            if (this.userID > -1)
            {
                SiteUser user = new SiteUser(siteSettings, this.userID);
                user.ApprovedForLogin = true;
                user.Save();

                //send user notification of approval
                if (WebConfigSettings.NotifyUsersOnAccountApproval)
                {
                    CultureInfo defaultCulture = SiteUtils.GetDefaultCulture();
                    string signInLink = SiteUtils.GetNavigationSiteRoot() + "/Secure/Login.aspx";
                    SmtpSettings smtpSettings = SiteUtils.GetSmtpSettings();
                    EmailMessageTask messageTask = new EmailMessageTask(smtpSettings);

                    messageTask.EmailFrom = siteSettings.DefaultEmailFromAddress;
                    messageTask.EmailFromAlias = siteSettings.DefaultFromEmailAlias;
                    messageTask.EmailTo = user.Email;

                    string subjectFormat = ResourceHelper.GetResourceString("Resource", "AccountApprovedSubjectformat", defaultCulture, true);
                    messageTask.Subject = string.Format(defaultCulture, subjectFormat, siteSettings.SiteName);

                    string textBodyTemplate = ResourceHelper.GetMessageTemplate(defaultCulture, "AccountApprovedMessage.config");
                    messageTask.TextBody = string.Format(
                        defaultCulture,
                        textBodyTemplate,
                        siteSettings.SiteName,
                        signInLink
                        );

                    messageTask.SiteGuid = siteSettings.SiteGuid;
                    messageTask.QueueTask();
                    WebTaskManager.StartOrResumeTasks();
                }
                

            }

            WebUtils.SetupRedirect(this, Request.RawUrl);
            return;
        }

        protected void btnUnlockUser_Click(object sender, EventArgs e)
        {
            if (this.userID > -1)
            {
                SiteUser user = new SiteUser(siteSettings, this.userID);
                user.UnlockAccount();
            }

            WebUtils.SetupRedirect(this, Request.RawUrl);
            return;
        }

        protected void btnLockUser_Click(object sender, EventArgs e)
        {
            if (this.userID > -1)
            {
                SiteUser user = new SiteUser(siteSettings, this.userID);
                user.LockoutAccount();
            }

            WebUtils.SetupRedirect(this, Request.RawUrl);
            return;
        }

        void btnPurgeUserLocations_Click(object sender, EventArgs e)
        {
            if (this.userID > -1)
            {
                SiteUser user = new SiteUser(siteSettings, this.userID);
                UserLocation.DeleteByUser(user.UserGuid);
            }

            WebUtils.SetupRedirect(this, Request.RawUrl);
            return;
        }

		private void AddRole_Click(Object sender, EventArgs e) 
		{
            if (this.userID > -1)
            {
                SiteUser user = new SiteUser(siteSettings, this.userID);
                int roleID = int.Parse(allRoles.SelectedItem.Value, CultureInfo.InvariantCulture);
                Role role = new Role(roleID);
                Role.AddUser(roleID, userID, role.RoleGuid, user.UserGuid);
                
            }
			
			WebUtils.SetupRedirect(this, Request.RawUrl);
		}


        private void UserRoles_ItemCommand(object sender, DataListCommandEventArgs e) 
		{
            int roleID = Convert.ToInt32(userRoles.DataKeys[e.Item.ItemIndex]);
			Role.RemoveUser(roleID,userID);
            userRoles.EditItemIndex = -1;
			WebUtils.SetupRedirect(this, Request.RawUrl);
			return;

        }


        void userRoles_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ImageButton btnRemoveRole = e.Item.FindControl("btnRemoveRole") as ImageButton;
            UIHelper.AddConfirmationDialog(btnRemoveRole, Resource.ManageUsersRemoveRoleWarning);
        }


        void btnConfirmEmail_Click(object sender, EventArgs e)
        {
            if (this.userID > 0)
            {
                SiteUser user = new SiteUser(siteSettings, this.userID);
                SiteUser.ConfirmRegistration(user.RegisterConfirmGuid);
            }

            WebUtils.SetupRedirect(this, Request.RawUrl);
            return;
            
        }

        private void PopulateLabels()
        {

            if (userID > -1)
            {
                Title = SiteUtils.FormatPageTitle(siteSettings, Resource.ManageUsersPageTitle);
                if (WebUser.IsInRoles(siteSettings.RolesThatCanManageUsers))
                {
                    liNewsletters.Visible = WebConfigSettings.EnableNewsletter;
                    tabNewsletters.Visible = liNewsletters.Visible;
                    newsLetterPrefs.Visible = liNewsletters.Visible;
                }
                btnUpdate.Text = Resource.ManageUsersUpdateButton;
                btnUpdate.ToolTip = Resource.ManageUsersUpdateButton;
            }
            else
            {
                Title = SiteUtils.FormatPageTitle(siteSettings, Resource.ManageUsersAddUserLabel);
                btnUpdate.Text = Resource.ManageUsersCreateButton;
                btnUpdate.ToolTip = Resource.ManageUsersCreateButton;
            }

            litSecurityTab.Text = "<a href='#tabSecurity'>" + Resource.ManageUsersSecurityTab + "</a>";
            litProfileTab.Text = "<a href='#" + tabProfile.ClientID + "'>" + Resource.ManageUsersProfileTab + "</a>";
            litOrderHistoryTab.Text = "<a href='#" + tabOrderHistory.ClientID + "'>" + Resource.CommerceOrderHistoryTab + "</a>";
            litNewsletterTab.Text = "<a href='#" + tabNewsletters.ClientID + "'>" + Resource.ManageUsersNewslettersTab + "</a>";
            litRolesTab.Text = "<a href='#" + tabRoles.ClientID + "'>" + Resource.ManageUsersRolesTab + "</a>";
            litActivityTab.Text = "<a href='#" + tabActivity.ClientID + "'>" + Resource.ManageUsersActivityTab + "</a>";
            litLocationTab.Text = "<a href='#" + tabLocation.ClientID + "'>" + Resource.ManageUsersLocationTab + "</a>";


            btnApprove.Text = Resource.ApproveUserButton;
            btnApprove.ToolTip = Resource.ApproveUserButton;

            btnUnlockUser.Text = Resource.UserUnlockUserButton;
            btnUnlockUser.ToolTip = Resource.UserUnlockUserButton;
            SiteUtils.SetButtonAccessKey(btnUnlockUser, AccessKeys.UserUnlockUserButtonAccessKey);

            btnLockUser.Text = Resource.UserLockUserButton;
            btnLockUser.ToolTip = Resource.UserLockUserButton;
            SiteUtils.SetButtonAccessKey(btnLockUser, AccessKeys.UserLockUserButtonAccessKey);

            btnConfirmEmail.Text = Resource.ManageUsersConfirmEmailButton;
            btnResendConfirmationEmail.Text = Resource.ResendConfirmationEmailButton;

            
            SiteUtils.SetButtonAccessKey(btnUpdate, AccessKeys.ManageUsersUpdateButtonAccessKey);

            btnDelete.Text = Resource.ManageUsersDeleteButton;
            btnDelete.ToolTip = Resource.ManageUsersDeleteButton;
            SiteUtils.SetButtonAccessKey(btnDelete, AccessKeys.ManageUsersDeleteButtonAccessKey);
            UIHelper.AddConfirmationDialog(btnDelete, Resource.ManageUsersDeleteUserWarning);

            addExisting.Text = Resource.ManageUsersAddToRoleButton;
            addExisting.ToolTip = Resource.ManageUsersAddToRoleButton;
            SiteUtils.SetButtonAccessKey(addExisting, AccessKeys.ManageUsersAddToRoleButtonAccessKey);

            lnkUnsubscribeFromForums.Text = Resource.ManageUsersUnsubscribeForumsLink;

            lnkAvatarUpload.Text = Resource.UploadAvatarLink;

            if (!(this.userID > -1))
            {
                this.btnUnlockUser.Enabled = false;
            }

            if (!IsPostBack)
            {
                txtPasswordQuestion.Text = Resource.ManageUsersDefaultSecurityQuestion;
                txtPasswordAnswer.Text = Resource.ManageUsersDefaultSecurityAnswer;
            }

            rfvName.ErrorMessage = Resource.UserProfileNameRequired;
            rfvLoginName.ErrorMessage = Resource.UserProfileLoginNameRequired;

            regexEmail.ErrorMessage = Resource.UserProfileEmailValidation;
            rfvEmail.ErrorMessage = Resource.UserProfileEmailRequired;

            if (siteSettings.UseLdapAuth && !WebConfigSettings.UseLDAPFallbackAuthentication)
            {
                this.divPassword.Visible = false;
            }

            if (allowGravatars)
            {
                gravatar1.Visible = true;
                imgAvatar.Visible = false;
                avatarHelp.Visible = false;
                lnkAvatarUpload.Visible = false;
               
            }
            else
            {
                gravatar1.Visible = false;

                if (disableAvatars)
                {
                    divAvatarUrl.Visible = false;
                    imgAvatar.Visible = false;
                    lnkAvatarUpload.Visible = false;
                    avatarHelp.Visible = false;
             
                }
                else
                {
                    imgAvatar.Visible = true;
                    avatarHelp.Visible = true;
                }

            }

            grdUserLocation.Columns[0].HeaderText = Resource.ManageUsersLocationGridIPAddressHeading;
            grdUserLocation.Columns[1].HeaderText = Resource.ManageUsersLocationGridHostnameHeading;
            grdUserLocation.Columns[2].HeaderText = Resource.ManageUsersLocationGridISPHeading;
            grdUserLocation.Columns[3].HeaderText = Resource.ManageUsersLocationGridContinentHeading;
            grdUserLocation.Columns[4].HeaderText = Resource.ManageUsersLocationGridCountryHeading;
            grdUserLocation.Columns[5].HeaderText = Resource.ManageUsersLocationGridRegionHeading;
            grdUserLocation.Columns[6].HeaderText = Resource.ManageUsersLocationGridCityHeading;
            grdUserLocation.Columns[7].HeaderText = Resource.ManageUsersLocationGridTimeZoneHeading;
            grdUserLocation.Columns[8].HeaderText = Resource.ManageUsersLocationGridCaptureCountHeading;
            grdUserLocation.Columns[9].HeaderText = Resource.ManageUsersLocationGridFirstCaptureHeading;
            grdUserLocation.Columns[10].HeaderText = Resource.ManageUsersLocationGridLastCaptureHeading;

            btnPurgeUserLocations.Text = Resource.PurgeUserLocations;
            UIHelper.AddConfirmationDialog(btnPurgeUserLocations, Resource.PurgeUserLocationsWarning);
            
           
        }

        protected void HideExtendedProfileControls()
        {
            tabActivity.Visible = false;
            tabProfile.Visible = false;
            tabRoles.Visible = false;
            liProfile.Visible = false;
            liOrderHistory.Visible = false;
            tabOrderHistory.Visible = false;
            liNewsletters.Visible = false;
            liRoles.Visible = false;
            liActivity.Visible = false;
            liLocation.Visible = false;


            divCreatedDate.Visible = false;
            divUserGuid.Visible = false;
            divTotalPosts.Visible = false;
            divProfileApproved.Visible = false;
            divApprovedForLogin.Visible = false;
            divTrusted.Visible = false;
            divDisplayInMemberList.Visible = false;
            divAvatarUrl.Visible = false;
            divRoles.Visible = false;
            divUserRoles.Visible = false;
            
            btnDelete.Visible = false;
            divLiveMessenger.Visible = false;
            
            this.divLastActivity.Visible = false;
            this.divLastLogin.Visible = false;
            this.divPasswordChanged.Visible = false;
            this.divLockoutDate.Visible = false;
            divEmailConfirm.Visible = false;
            this.divFailedPasswordAttempt.Visible = false;
            this.divFailedPasswordAnswerAttempt.Visible = false;
            this.divLockout.Visible = false;
            this.divComment.Visible = false;
            this.divOpenID.Visible = false;
            this.divWindowsLiveID.Visible = false;
            this.tabNewsletters.Visible = false;
            newsLetterPrefs.Visible = false;
            this.tabLocation.Visible = false;


        }

        private void LoadSettings()
        {
            
            currentUser = SiteUtils.GetCurrentSiteUser();
            isAdmin = WebUser.IsAdmin;
            pageID = WebUtils.ParseInt32FromQueryString("pageid", -1);
            userID = WebUtils.ParseInt32FromQueryString("userid", true, userID);
            TimeOffset = SiteUtils.GetUserTimeOffset();
            timeZone = SiteUtils.GetUserTimeZone();
            userGuid = WebUtils.ParseGuidFromQueryString("u", Guid.Empty);
#if!MONO
            divTimeZone.Visible = true;

#endif

            switch (siteSettings.AvatarSystem)
            {
                case "gravatar":
                    allowGravatars = true;
                    disableAvatars = true;
                    break;

                case "internal":
                    allowGravatars = false;
                    disableAvatars = false;
                    lnkAvatarUpload.NavigateUrl = SiteRoot + "/Dialog/AvatarUploadDialog.aspx?u=" + userID.ToInvariantString();
                    lnkAvatarUpload.ClientClick = "return GB_showPage('" + Page.Server.HtmlEncode(Resource.UploadAvatarAdminHeading) + "', this.href, GBCallback)";
                    break;

                case "none":
                default:
                    allowGravatars = false;
                    disableAvatars = true;
                    break;

            }


            lnkUnsubscribeFromForums.Visible = WebConfigSettings.ShowForumUnsubscribeLinkInUserManagement;

            if (WebConfigSettings.MaskPasswordsInUserAdmin)
            {
                txtPassword.TextMode = TextBoxMode.Password;
            }

            regexEmail.ValidationExpression = SecurityHelper.RegexEmailValidationPattern;

            if (WebConfigSettings.UseRelatedSiteMode)
            {
                divTotalPosts.Visible = false;
            }

            commerceConfig = SiteUtils.GetCommerceConfig();
            if (!commerceConfig.IsConfigured)
            {
                liOrderHistory.Visible = false;
                tabOrderHistory.Visible = false;
            }

            string wlAppId = siteSettings.WindowsLiveAppId;
            if (ConfigurationManager.AppSettings["GlobalWindowsLiveAppId"] != null)
            {
                wlAppId = ConfigurationManager.AppSettings["GlobalWindowsLiveAppId"];
                if (wlAppId.Length == 0) { wlAppId = siteSettings.WindowsLiveAppId; }
            }

            if (
                (WebConfigSettings.GloballyDisableMemberUseOfWindowsLiveMessenger)
                || (!siteSettings.AllowWindowsLiveMessengerForMembers)
                || (wlAppId.Length == 0)
                )
            {
                divLiveMessenger.Visible = false;
            }

            if (userID == -1) { HideExtendedProfileControls(); }

            divReqPasswordChange.Visible = WebConfigSettings.AllowRequiringPasswordChange;

            AddClassToBody("administration");
            AddClassToBody("manageusers");
        }

        private void SetupAvatarScript()
        {

            string callback = "<script type=\"text/javascript\">"
                + "function GBCallback() { "
                + " window.location.reload(true); "
                + "}</script>";

            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "gbcallback", callback);
        }
        

	}
}




